<?php defined('SYSPATH') or die('No direct access allowed.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/14 18:02
 * Desc: 产品订单数据统计
 */
class Model_Member_Order_statistic extends ORM
{
    // 每次都去重新计算
    public static function order_statistic($type_id, $product_id)
    {
        $sell_num = DB::select(DB::expr('SUM(ifnull(dingnum,0)+ifnull(childnum,0)+ifnull(oldnum,0)+ifnull(people4_num,0)+ifnull(people5_num,0)+ifnull(people6_num,0)) as num'))
            ->from('member_order')
            ->where('typeid', '=', $type_id)
            ->where('productautoid','=', $product_id)
            ->where('status', 'in', array(2,5,6,8))
            ->execute()
            ->current();
        $sell_num = $sell_num ? $sell_num['num']: 0;

        $member_order_statistic = ORM::factory('member_order_statistic')
            ->where('type_id', '=', $type_id)
            ->where('product_id', '=', $product_id)
            ->find();
        if(!$member_order_statistic->id)
        {
            $member_order_statistic->type_id = $type_id;
            $member_order_statistic->product_id = $product_id;
            $member_order_statistic->addtime = time();
        }
        $member_order_statistic->modtime = time();
        $member_order_statistic->sell_num = $sell_num;
        $member_order_statistic->save();
    }

    /**
     * 获取销售数量
     * @param $type_id
     * @param $product_id
     * @return int|mixed
     */
    public static function get_sell_num($type_id, $product_id)
    {
        $statistic = DB::select('sell_num')
            ->from('member_order_statistic')
            ->where('type_id', '=', $type_id)
            ->where('product_id', '=', $product_id)
            ->execute()
            ->current();

        return $statistic ? $statistic['sell_num'] : 0;
    }
}